package com.k.liuzh.extraffic;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;


public class SplashActivity extends AppCompatActivity {
    private static final String TAG = "SplashActivity";
    private Timer timer;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initData();
    }

    private void initData() {
        sharedPreferences = getSharedPreferences("login",MODE_PRIVATE);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Intent intent;
                Log.d(TAG, "run: 123123123"+sharedPreferences.getBoolean("isSuccess",false));
                if (sharedPreferences.getBoolean("isSuccess",false)){
                    String name = sharedPreferences.getString("userName",null);
                    intent = new Intent(SplashActivity.this,MainActivity.class);
                    intent.putExtra("userName",name);
                }else {
                    intent = new Intent(SplashActivity.this,LoginActivity.class);
                }
                startActivity(intent);
                finish();
            }
        },2000);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
